package com.android.tiango.baba.act;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.photoview.PhotoView;
import com.android.tiango.baba.view.HackyViewPager;
/**
 * 图片浏览处理
 * @author txg
 */
public class ActImgBrowse extends BaBaseActivity {
	private HackyViewPager mViewPager; 
	private String[] urls;
	private TextView mTvInfo;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0); 
		setContentView(R.layout.layout_browse);
		init();
	}

	private void init() {
		mViewPager = (HackyViewPager) findViewById(R.id.pager);
		mTvInfo = (TextView) findViewById(R.id.tv_infotag); 
		if (null != getIntent()) {
			urls = getIntent().getStringArrayExtra("urls");
			if (urls != null && urls.length > 0) {
				mTvInfo.setText(1+"/"+urls.length); 
				mViewPager.setAdapter(new SamplePagerAdapter()); 
				mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
					@Override
					public void onPageSelected(int arg0) {
							mTvInfo.setText(arg0+1+"/"+urls.length); 
					}
					
					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {}
					
					@Override
					public void onPageScrollStateChanged(int arg0) {
					}
				});
			} else {
				this.finish();
			}
		} else {
			this.finish();
		}
		// FragmentStatePagerAdapter
		
	}

	 class SamplePagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return urls.length;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			new ImageLoader(ActImgBrowse.this, R.drawable.bg_dau).loadImage(urls[position], photoView);
			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
	/**
	 class SamplePagerAdapter extends FragmentStatePagerAdapter {

		public SamplePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mList.get(arg0);
		}

		@Override
		public int getCount() {
			return mList.size();
		} 
	} */
}
